<!-- 当前组件是weather页面的子组件  弹出窗口显示未来天气的echarts图表 -->
<template>
    <div class="box">
        <div class="header">
            <div class="top">
                <span>{{ new Date().toLocaleString() }}</span>
                <div class="btn" @click="clickHandler">×</div>
            </div>
            <div class="bottom">
                <div class="title">
                    <div class="p"></div>
                    <span>未来{{ dateList.length }}天天气</span>
                </div>
                <div class="tags">
                    <span>未来{{ dateList.length }}天变化</span>
                </div>
            </div>
        </div>
        <div class="echarts" ref="reportRef">
            <!-- 放图表 -->
        </div>
        <div class="main">
            <ul class="name">
                <li>日期</li>
                <li>温度</li>
                <li>天气</li>
                <li>风向</li>
            </ul>
            <ul class="item" v-for="(value, index) in future" :key="index">
                <li>{{ (value as IFutureItem).date }}</li>
                <li>{{ (value as IFutureItem).temperature }}</li>
                <li>{{ (value as IFutureItem).weather }}</li>
                <li>{{ (value as IFutureItem).direct }}</li>
            </ul>
        </div>
    </div>
</template>



<script lang='ts' setup>
import { onBeforeMount, onMounted, ref, type ModelRef } from 'vue';
import * as echarts from 'echarts';
import type { IFutureItem } from '@/interface/IWeather';
type EChartsOption = echarts.EChartsOption;
// 预报组件开关
const reportFlag: ModelRef<boolean> = defineModel("reportFlag", { required: true })
let dateList: string[] = []
let temDayList: string[] = []
let temNightList: string[] = []
// 接收父组件传递过来的预报列表
const props = defineProps({
    future: {
        required: true,
        type: Array
    }
})
onBeforeMount(() => {
    // 日期数组  温度数组
    props.future.forEach((item) => {
        dateList.push((item as IFutureItem).date)
        // 温度不能直接加，要修改一下再push
        temDayList.push((item as IFutureItem).temperature.split('/')[0])
        temNightList.push((item as IFutureItem).temperature.split('/')[1].split('℃')[0])
    })
})

const reportRef = ref(null)
onMounted(() => {

    // 图表dom
    var myChart = echarts.init(reportRef.value);
    var option: EChartsOption;
    option = {
        tooltip: {
            trigger: 'axis'
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: [...dateList]//这改成日期
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name: 'low',
                data: [...temDayList], //这改成温度
                type: 'line'
            },
            {
                name: 'hight',
                data: [...temNightList], //这改成温度
                type: 'line'
            }
        ]
    };

    option && myChart.setOption(option);
})

const clickHandler = () => {
    if (reportFlag.value) reportFlag.value = false
}
</script>

<style lang='scss' scoped>
.box {
    height: 100%;
    width: 500px;
    background-color: #182A31;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;

    li {
        list-style: none;
    }

    .header {
        width: 100%;
        height: 150px;
        border-bottom: 1px solid grey;
        color: white;
        font-size: 14px;

        .top {
            height: 50px;
            width: 100%;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid grey;

            span,
            .btn {
                display: block;
                height: 30px;
                text-align: center;
                line-height: 30px;
                cursor: pointer;
            }

            span {
                width: 200px;

            }

            .btn {
                width: 30px;
                font-size: 20px;

                margin-right: 10px;
            }
        }

        .bottom {
            height: 80px;
            width: 100%;

            .title {
                height: 30px;
                width: 100%;
                display: flex;
                align-items: center;
                margin-top: 10px;

                .p {
                    height: 20px;
                    width: 3px;
                    background-color: #52E1DD;
                    margin-left: 20px;
                }

                span {
                    display: block;
                    color: white;
                    margin-left: 10px;
                }
            }

            .tags {
                height: 50px;
                width: 100%;
                display: flex;
                justify-content: center;
                align-items: center;

                span {
                    display: block;
                    height: 25px;
                    width: 100px;
                    text-align: center;
                    line-height: 25px;
                    border-radius: 3px;
                    background-color: #52E1DD;
                    font-size: 12px;
                    color: #295B60;
                }
            }
        }
    }

    .echarts {
        height: 220px;
        width: 100%;
    }

    .main {
        height: 300px;
        width: 100%;
        display: flex;

        ul {
            height: 100%;
            width: 20%;
            border-right: 1px solid gray;
            display: flex;
            flex-direction: column;
            justify-content: space-around;
            align-items: start;
            margin: 0;
            padding: 0;
            color: white;

            li {

                width: 100%;
                height: 30px;
                font-size: 13px;
                text-align: center;
                line-height: 30px;
            }
        }

        .name {
            color: #52E1DD;
        }
    }
}
</style>